import time
from . import ConnPG
import psycopg2
from sanic.log import logger

class WarehouseTable:

    async def add_warehouse(self, warehouse_name):#添加库房

        #参数检查

        if not warehouse_name:

            return {
                'status': -1,
                'errorCode': 300100,
                'errorMsg': '库房名不能为空',
                'resultBody': None
            }

        sql = 'INSERT INTO warehouse(warehouse_name) VALUES (%s)'

        try:
            await ConnPG().execute(sql, (warehouse_name,))
        except psycopg2.errors.UniqueViolation:

            return {
                'status': -1,
                'errorCode': 300101,
                'errorMsg': '当前库名已经存在',
                'resultBody': None,
            }
        else:

            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "库房添加成功",
            }

    async def select_all_warehouse(self):#查看所有库房

        sql = 'select * from warehouse'

        result = await ConnPG().fetchall(sql)

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }


    async def select_all_warehouse_by_duty(self, duty_id):

        if duty_id == None or not duty_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 300107,
                'errorMsg': '却少duty_id参数',
                'resultBody': None
            }



        sql_ = "select * from view_duty_warehouse where duty_id=%s"

        duty_result = await ConnPG().fetchall(sql_, (duty_id,))

        warehouse_list = [i['warehouse_id'] for i in duty_result]

        warehouse_list_str = ''

        for i in warehouse_list:

            warehouse_list_str = warehouse_list_str + f'or id={i} '

        sql = "select * from warehouse where " + warehouse_list_str[2:]

        result = await ConnPG().fetchall(sql)
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }




    async def del_warehouse(self, warehouse_id):#删除warehouse

        #参数检查

        if not warehouse_id or not warehouse_id.isdecimal:

            return {
                'status': -1,
                'errorCode': 300100,
                'errorMsg': '库房id不能为空且为整数类型',
                'resultBody': None
            }

        sql = 'DELETE FROM warehouse WHERE id=%s'

        try:
            await ConnPG().execute(sql, (warehouse_id,))
        except psycopg2.errors.ForeignKeyViolation as e:


            return {

                'status': -1,
                'errorCode': 300112,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "公司或项目删除成功",                                     
            }

    async def update_warehouse(self, warehouse_id, warehouse_name):#更新库房名称

        #参数检查
        if not all([warehouse_id, warehouse_name]) or not warehouse_id.isdecimal():


            return {
                'status': -1,
                'errorCode': 300100,
                'errorMsg': '接口有两个必传参数且warehouse_id为整数',
                'resultBody': None
            }

        sql = "UPDATE warehouse SET warehouse_name=%s WHERE id=%s"


        await ConnPG().execute(sql, (warehouse_name,warehouse_id))

    
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "名称修改成功",
        }

    